fix(backend): consolidate error handling with custom error classes#140
Open
Netty-kun wants to merge 1 commit into
Open
fix(backend): consolidate error handling with custom error classes#140Netty-kun wants to merge 1 commit into
Netty-kun wants to merge 1 commit into
Conversation
- Add AppError base class with statusCode, code, isOperational, details
- Add ValidationError (400), NotFoundError (404), UnauthorizedError (401), RateLimitError (429)
- Update errorHandler to handle all AppError subclasses
- Hide stack traces in production (NODE_ENV=production)
- Refactor auth middleware to throw UnauthorizedError
- Refactor all routes to use custom error classes via next(err)
- Update tests for new consistent error shape
- All errors now return { error: { code, message, details? } }
Closes Epta-Node#109
|
@Netty-kun is attempting to deploy a commit to the Jaja's projects Team on Vercel. A member of the Team first needs to authorize it. |
devJaja
self-requested a review
July 20, 2026 23:39
Contributor
|
Nice Implementation @Netty-kun |
Contributor
|
@Netty-kun |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
This PR consolidates error handling across the backend by introducing a hierarchy of custom error classes. Instead of ad-hoc
res.status().json()calls and rawthrow new Error(), all routes now throw typed errors that are caught by the updated error handler middleware. This ensures consistent error response shapes everywhere.Related Issue
Closes #109
Changes
🏗️ Custom Error Classes
backend/src/errors/AppError.ts— Base class withstatusCode,code,isOperational,detailsbackend/src/errors/ValidationError.ts— 400,VALIDATION_ERRORcode, supports field detailsbackend/src/errors/NotFoundError.ts— 404,NOT_FOUNDcode, supports resource name + optional IDbackend/src/errors/UnauthorizedError.ts— 401,UNAUTHORIZEDcode, accepts custom messagebackend/src/errors/RateLimitError.ts— 429,RATE_LIMITEDcodebackend/src/errors/index.ts— Barrel exports⚙️ Error Handler Middleware
backend/src/api/middleware/errorHandler.tsAppErrorsubclasses and returns correct HTTP status codeswarnlevel (no stack trace) vs non-operational aterrorlevelNODE_ENV=production)Errorobjects🔐 Auth & Routes
backend/src/api/middleware/auth.ts— Callsnext(new UnauthorizedError())instead ofres.status(401).json()backend/src/api/app.ts— All inline route handlers usenext(new ValidationError | NotFoundError | UnauthorizedError | AppError(...));defaultDispatchthrowsAppErrorinstead ofErrorbackend/src/api/routes/agents.ts— All error paths usenext()with custom error classesbackend/src/api/routes/tasks.ts— Same pattern appliedbackend/src/api/routes/stats.ts— Same pattern applied🧪 Tests
backend/tests/middleware.test.ts— Auth tests now verifynextis called with error (statusCode 401)backend/tests/agents.test.ts— Updated expectations for{ error: { code, message } }shape; added test error handlerVerification Results
backend/src/errors/Errorobjects thrown in routes{ error: { code, message, details? } }